<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\Request;
use think\db\Query;
/**
 * 首页接口
 */
class Sundrieslift extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function list(){
        $data = Db::name('sundries_lift_month')->select();
        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    public function recording_list(){
        $data = Db::name('recording_sundries_lift_month')->column('elevator_id');
        $list = Db::name('elevator_basic_info')->where('id','in',$data)->select();
        return json_encode($list,JSON_UNESCAPED_UNICODE);
    }

    public function recording_detailed(){
        $title = Db::name('sundries_lift_month')->select();
        $data = input('post.');
        $list = Db::name('recording_sundries_lift_month')->where('elevator_id',$data['id'])->order('id desc')->find();
        foreach ($list as $key => $value) {
            if (preg_match('/^\_\d+$/', $key)) {
                $title[((int)preg_replace('/\_/', '', $key))-1]['conclusion'] = $value;
            }
        }
        return json_encode(['data'=>$title,'list'=>$list],JSON_UNESCAPED_UNICODE);
    }


public function upload()
{
    // 接收前端POST请求发送的图片路径
    $requestData = Request::instance()->post(); // 获取POST请求的所有数据
    $signaturePath = $requestData['image_path']; // 从请求数据中提取image_path字段的值，即图片路径

    // 使用Db类执行插入操作，将图片路径保存到signature_one字段
    $result = Db::name('recording_sundries_lift_month') // 指定表名
              ->insert([ // 准备插入的数据
                  'signature_one' => $signaturePath, // 将图片路径插入到signature_one字段
                  // 这里可以根据需要添加更多字段及其对应的值
              ]);

    // 判断插入操作是否成功
    if ($result) {
        // 如果插入成功，返回成功状态和消息
        return json(['status' => 'success', 'message' => '签名图片路径保存成功']);
    } else {
        // 如果插入失败，返回失败状态和消息
        return json(['status' => 'fail', 'message' => '签名图片路径保存失败，请重试']);
    }
}
     public function upload2()
{
    // 接收前端发送的图片路径和电梯ID
    $data = Request::instance()->post();
    $signaturePath = $data['image_path'];
    $elevatorId = $data['elevator_id'];

    // 使用Db门面进行数据更新操作
    // 更新满足elevator_id条件的最后一项记录
    $result = Db::name('recording_sundries_lift_month')
               ->where('elevator_id', $elevatorId)
               ->order('id desc') // 确保按ID降序排列
               ->limit(1) // 仅更新符合条件的第一条记录
               ->update(['signature_two' => $signaturePath]);

    if ($result !== false) {
        return json(['status' => 'success', 'message' => '签名图片路径更新成功']);
    } else {
        return json(['status' => 'fail', 'message' => '签名图片路径更新失败，请重试']);
    }
}
         public function upload3()
    {
        // 接收前端发送的图片路径和电梯ID
        $data = Request::instance()->post();
        $signaturePath = $data['image_path'];
        $elevatorId = $data['elevator_id'];

        // 使用Db门面进行数据更新操作
        // 更新满足elevator_id条件的最后一项记录
        $result = Db::name('recording_sundries_lift_month')
                   ->where('elevator_id', $elevatorId)
                   ->order('id desc') // 确保按ID降序排列
                   ->limit(1) // 仅更新符合条件的第一条记录
                   ->update(['signature_manage' => $signaturePath]);

        if ($result !== false) {
            return json(['status' => 'success', 'message' => '签名图片路径更新成功']);
        } else {
            return json(['status' => 'fail', 'message' => '签名图片路径更新失败，请重试']);
        }
    }
    public function saveConclusion()
{
    $data = request()->post('records/a');
    $recording = [];
    foreach ($data['recording']['data'] as $v) {
        $recording[$v['recording_id']] = $v['conclusion'];
    }
    foreach ($data['text']['data'] as $v) {
        $recording[strval($v['type'])] = $v['value'];
    }
    $recording['elevator_id'] = $data['recording']['id'];
    $result = Db::name('recording_sundries_lift_month')->insert($recording);
    if ($result !== false) {
        return '添加成功';
    } else {
        return '添加失败';
    }
}
    public function list_one(){
        $data = Db::name('sundries_lift_quarter')->select();
        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    public function recording_quarter_list(){
        $data = Db::name('recording_sundries_lift_quarter')->column('elevator_id');
        $list = Db::name('elevator_basic_info')->where('id','in',$data)->select();
        return json_encode($list,JSON_UNESCAPED_UNICODE);
    }

    public function recording_quarter_detailed(){
        $title = Db::name('sundries_lift_quarter')->select();
        $data = input('post.');
        $list = Db::name('recording_sundries_lift_quarter')->where('elevator_id',$data['id'])->order('id desc')->find();
        foreach ($list as $key => $value) {
            if (preg_match('/^\_\d+$/', $key)) {
                $title[((int)preg_replace('/\_/', '', $key))-1]['conclusion'] = $value;
            }
        }
        return json_encode(['data'=>$title,'list'=>$list],JSON_UNESCAPED_UNICODE);
    }

public function saveConclusion_one()
{
    $data = request()->post('records/a');
    $recording = [];
    foreach ($data['recording']['data'] as $v) {
        $recording[$v['recording_id']] = $v['conclusion'];
    }
    foreach ($data['text']['data'] as $v) {
        $recording[strval($v['type'])] = $v['value'];
    }
    $recording['elevator_id'] = $data['recording']['id'];
    $result = Db::name('recording_sundries_lift_quarter')->insert($recording);
    if ($result !== false) {
        return '添加成功';
    } else {
        return '添加失败';
    }
}
public function upload_one()
{
    // 接收前端POST请求发送的图片路径
    $requestData = Request::instance()->post(); // 获取POST请求的所有数据
    $signaturePath = $requestData['image_path']; // 从请求数据中提取image_path字段的值，即图片路径

    // 使用Db类执行插入操作，将图片路径保存到signature_one字段
    $result = Db::name('recording_sundries_lift_quarter') // 指定表名
              ->insert([ // 准备插入的数据
                  'signature_one' => $signaturePath, // 将图片路径插入到signature_one字段
                  // 这里可以根据需要添加更多字段及其对应的值
              ]);

    // 判断插入操作是否成功
    if ($result) {
        // 如果插入成功，返回成功状态和消息
        return json(['status' => 'success', 'message' => '签名图片路径保存成功']);
    } else {
        // 如果插入失败，返回失败状态和消息
        return json(['status' => 'fail', 'message' => '签名图片路径保存失败，请重试']);
    }
}
     public function upload2_one()
{
    // 接收前端发送的图片路径和电梯ID
    $data = Request::instance()->post();
    $signaturePath = $data['image_path'];
    $elevatorId = $data['elevator_id'];

    // 使用Db门面进行数据更新操作
    // 更新满足elevator_id条件的最后一项记录
    $result = Db::name('recording_sundries_lift_quarter')
               ->where('elevator_id', $elevatorId)
               ->order('id desc') // 确保按ID降序排列
               ->limit(1) // 仅更新符合条件的第一条记录
               ->update(['signature_two' => $signaturePath]);

    if ($result !== false) {
        return json(['status' => 'success', 'message' => '签名图片路径更新成功']);
    } else {
        return json(['status' => 'fail', 'message' => '签名图片路径更新失败，请重试']);
    }
}
        public function upload3_one()
    {
        // 接收前端发送的图片路径和电梯ID
        $data = Request::instance()->post();
        $signaturePath = $data['image_path'];
        $elevatorId = $data['elevator_id'];

        // 使用Db门面进行数据更新操作
        // 更新满足elevator_id条件的最后一项记录
        $result = Db::name('recording_sundries_lift_quarter')
                   ->where('elevator_id', $elevatorId)
                   ->order('id desc') // 确保按ID降序排列
                   ->limit(1) // 仅更新符合条件的第一条记录
                   ->update(['signature_manage' => $signaturePath]);

        if ($result !== false) {
            return json(['status' => 'success', 'message' => '签名图片路径更新成功']);
        } else {
            return json(['status' => 'fail', 'message' => '签名图片路径更新失败，请重试']);
        }
    }

    
    public function list_two(){
        $data = Db::name('sundries_lift_half')->select();
        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    public function recording_half_list(){
        $data = Db::name('recording_sundries_lift_half')->column('elevator_id');
        $list = Db::name('elevator_basic_info')->where('id','in',$data)->select();
        return json_encode($list,JSON_UNESCAPED_UNICODE);
    }

    public function recording_half_detailed(){
        $title = Db::name('sundries_lift_half')->select();
        $data = input('post.');
        $list = Db::name('recording_sundries_lift_half')->where('elevator_id',$data['id'])->order('id desc')->find();
        foreach ($list as $key => $value) {
            if (preg_match('/^\_\d+$/', $key)) {
                $title[((int)preg_replace('/\_/', '', $key))-1]['conclusion'] = $value;
            }
        }
        return json_encode(['data'=>$title,'list'=>$list],JSON_UNESCAPED_UNICODE);
    }

public function saveConclusion_two()
{
    $data = request()->post('records/a');
    $recording = [];
    foreach ($data['recording']['data'] as $v) {
        $recording[$v['recording_id']] = $v['conclusion'];
    }
    foreach ($data['text']['data'] as $v) {
        $recording[strval($v['type'])] = $v['value'];
    }
    $recording['elevator_id'] = $data['recording']['id'];
    $result = Db::name('recording_sundries_lift_half')->insert($recording);
    if ($result !== false) {
        return '添加成功';
    } else {
        return '添加失败';
    }
}
public function upload_two()
{
    // 接收前端POST请求发送的图片路径
    $requestData = Request::instance()->post(); // 获取POST请求的所有数据
    $signaturePath = $requestData['image_path']; // 从请求数据中提取image_path字段的值，即图片路径

    // 使用Db类执行插入操作，将图片路径保存到signature_one字段
    $result = Db::name('recording_sundries_lift_half') // 指定表名
              ->insert([ // 准备插入的数据
                  'signature_one' => $signaturePath, // 将图片路径插入到signature_one字段
                  // 这里可以根据需要添加更多字段及其对应的值
              ]);

    // 判断插入操作是否成功
    if ($result) {
        // 如果插入成功，返回成功状态和消息
        return json(['status' => 'success', 'message' => '签名图片路径保存成功']);
    } else {
        // 如果插入失败，返回失败状态和消息
        return json(['status' => 'fail', 'message' => '签名图片路径保存失败，请重试']);
    }
}
         public function upload2_two()
    {
        // 接收前端发送的图片路径和电梯ID
        $data = Request::instance()->post();
        $signaturePath = $data['image_path'];
        $elevatorId = $data['elevator_id'];

        // 使用Db门面进行数据更新操作
        // 更新满足elevator_id条件的最后一项记录
        $result = Db::name('recording_sundries_lift_half')
                   ->where('elevator_id', $elevatorId)
                   ->order('id desc') // 确保按ID降序排列
                   ->limit(1) // 仅更新符合条件的第一条记录
                   ->update(['signature_two' => $signaturePath]);

        if ($result !== false) {
            return json(['status' => 'success', 'message' => '签名图片路径更新成功']);
        } else {
            return json(['status' => 'fail', 'message' => '签名图片路径更新失败，请重试']);
        }
    }
         public function upload3_two()
    {
        // 接收前端发送的图片路径和电梯ID
        $data = Request::instance()->post();
        $signaturePath = $data['image_path'];
        $elevatorId = $data['elevator_id'];

        // 使用Db门面进行数据更新操作
        // 更新满足elevator_id条件的最后一项记录
        $result = Db::name('recording_sundries_lift_half')
                   ->where('elevator_id', $elevatorId)
                   ->order('id desc') // 确保按ID降序排列
                   ->limit(1) // 仅更新符合条件的第一条记录
                   ->update(['signature_manage' => $signaturePath]);

        if ($result !== false) {
            return json(['status' => 'success', 'message' => '签名图片路径更新成功']);
        } else {
            return json(['status' => 'fail', 'message' => '签名图片路径更新失败，请重试']);
        }
    }


    public function list_three(){
        $data = Db::name('sundries_lift_year')->select();
        return json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    public function recording_year_list(){
        $data = Db::name('recording_sundries_lift_year')->column('elevator_id');
        $list = Db::name('elevator_basic_info')->where('id','in',$data)->select();
        return json_encode($list,JSON_UNESCAPED_UNICODE);
    }

    public function recording_year_detailed(){
        $title = Db::name('sundries_lift_year')->select();
        $data = input('post.');
        $list = Db::name('recording_sundries_lift_year')->where('elevator_id',$data['id'])->order('id desc')->find();
        foreach ($list as $key => $value) {
            if (preg_match('/^\_\d+$/', $key)) {
                $title[((int)preg_replace('/\_/', '', $key))-1]['conclusion'] = $value;
            }
        }
        return json_encode(['data'=>$title,'list'=>$list],JSON_UNESCAPED_UNICODE);
    }


public function upload_three()
{
    // 接收前端POST请求发送的图片路径
    $requestData = Request::instance()->post(); // 获取POST请求的所有数据
    $signaturePath = $requestData['image_path']; // 从请求数据中提取image_path字段的值，即图片路径

    // 使用Db类执行插入操作，将图片路径保存到signature_one字段
    $result = Db::name('recording_sundries_lift_year') // 指定表名
              ->insert([ // 准备插入的数据
                  'signature_one' => $signaturePath, // 将图片路径插入到signature_one字段
                  // 这里可以根据需要添加更多字段及其对应的值
              ]);

    // 判断插入操作是否成功
    if ($result) {
        // 如果插入成功，返回成功状态和消息
        return json(['status' => 'success', 'message' => '签名图片路径保存成功']);
    } else {
        // 如果插入失败，返回失败状态和消息
        return json(['status' => 'fail', 'message' => '签名图片路径保存失败，请重试']);
    }
}
     public function upload2_three()
{
    // 接收前端发送的图片路径和电梯ID
    $data = Request::instance()->post();
    $signaturePath = $data['image_path'];
    $elevatorId = $data['elevator_id'];

    // 使用Db门面进行数据更新操作
    // 更新满足elevator_id条件的最后一项记录
    $result = Db::name('recording_sundries_lift_year')
               ->where('elevator_id', $elevatorId)
               ->order('id desc') // 确保按ID降序排列
               ->limit(1) // 仅更新符合条件的第一条记录
               ->update(['signature_two' => $signaturePath]);

    if ($result !== false) {
        return json(['status' => 'success', 'message' => '签名图片路径更新成功']);
    } else {
        return json(['status' => 'fail', 'message' => '签名图片路径更新失败，请重试']);
    }
}
         public function upload3_three()
    {
        // 接收前端发送的图片路径和电梯ID
        $data = Request::instance()->post();
        $signaturePath = $data['image_path'];
        $elevatorId = $data['elevator_id'];

        // 使用Db门面进行数据更新操作
        // 更新满足elevator_id条件的最后一项记录
        $result = Db::name('recording_sundries_lift_year')
                   ->where('elevator_id', $elevatorId)
                   ->order('id desc') // 确保按ID降序排列
                   ->limit(1) // 仅更新符合条件的第一条记录
                   ->update(['signature_manage' => $signaturePath]);

        if ($result !== false) {
            return json(['status' => 'success', 'message' => '签名图片路径更新成功']);
        } else {
            return json(['status' => 'fail', 'message' => '签名图片路径更新失败，请重试']);
        }
    }

    public function saveConclusion_three()
    {
        $data = request()->post('records/a');
        $recording = [];
        foreach ($data['recording']['data'] as $v) {
            $recording[$v['recording_id']] = $v['conclusion'];
        }
        foreach ($data['text']['data'] as $v) {
            $recording[strval($v['type'])] = $v['value'];
        }
        $recording['elevator_id'] = $data['recording']['id'];
        $result = Db::name('recording_sundries_lift_year')->insert($recording);
        if ($result !== false) {
            return '添加成功';
        } else {
            return '添加失败';
        }
}
}